﻿using AutoMapper;
using B.S.BasicData.Api.Write.Application.Command.Equipment;
using B.S.BasicData.Domain.Entities;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructure.Interfaces;
using B.S.BasicData.Infrastructure.Utils;
using MediatR;
using Yitter.IdGenerator;

namespace B.S.BasicData.Api.Write.Application.CommandHandler.Account
{
    public class DelEquipmentHandler : IRequestHandler<DelEquipmentCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<EquipmentModel> accountRepo;  //仓储
        private readonly IMapper mapper; //mapper
        private readonly ILogger<DelEquipmentHandler> logger; //日志
        private readonly IBaseRepository<LogModel> logRepo;





        //注入--构造函数
        public DelEquipmentHandler(IBaseRepository<EquipmentModel> accountRepo, IMapper mapper, ILogger<DelEquipmentHandler> logger, IBaseRepository<LogModel> logRepo)
        {
            this.accountRepo = accountRepo;
            this.mapper = mapper;
            this.logger = logger;
            this.logRepo = logRepo;
        }
        /// <summary>
        /// 中介者-账号删除处理方法
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<ApiResult<int>> Handle(DelEquipmentCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();

            var model = accountRepo.GetModel(request.Id).Result; //先根据命令里的Id查到要删除的实体对象  .Result
            if (model == null)
            {//如果没有查到账号,
                result.Code = ApiEnums.Error;
                result.Msg = "要删除的账号不存在!";//ApiEnums.Error.Descrption();
                result.Data = 0;
                return Task.FromResult(result);//如果要删除的数据不存在,直接返回错误信息
            }
            model.IsDeleted = true;
            //model.IsDeleted = !model.IsDeleted;

            var r = accountRepo.UpdateAsync(model).Result;

            if (r > 0)
            {
                result.Code = ApiEnums.DelEquipmentOk;
                result.Msg = ApiEnums.DelEquipmentOk.Descrption();
                result.Data = r;
                //删除的日志
                logRepo.CreateAsync(new LogModel()
                {
                    Id = YitIdHelper.NextId(),
                    CreateName = model.EquipmentName,
                    LogContent = "逻辑删除账号成功",
                    CreatTime = DateTime.Now,
                });
            }
            else
            {
                result.Code = ApiEnums.Fail;
                result.Msg = ApiEnums.Fail.Descrption();


                //日志
                var logr = logRepo.CreateAsync(new LogModel()
                {
                    Id = YitIdHelper.NextId(),
                    CreateName = model.EquipmentName,
                    LogContent = "逻辑删除账号失败",
                    CreatTime = DateTime.Now,

                }).Result;

                result.Data = logr;

            }

            return Task.FromResult(result);
        }
    }
}
